package com.jiuyin.dianjing.ui.fragment.search;

import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.BaseRecyclerAdapter;
import com.jiuyin.dianjing.adapter.SmartViewHolder;
import com.jiuyin.dianjing.enums.SearchFragment;
import com.jiuyin.dianjing.event.MsgDispatcher;
import com.jiuyin.dianjing.event.WordMsgEvent;
import com.jiuyin.dianjing.ui.fragment.base.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;

import static android.R.layout.simple_list_item_1;
import static androidx.recyclerview.widget.DividerItemDecoration.VERTICAL;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_ANTISTOP;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;

/*
 *
 * 搜索内容列表界面
 */
public class SearchWordFragment extends BaseFragment {
    @BindView(R.id.rcv_page)
    RecyclerView mRcv;
    private BaseRecyclerAdapter<String> mAdapter;
    private WordMsgEvent mEvent;
    private String mType;

    public SearchWordFragment() {
        EventBus.getDefault().register(this);
    }

    /**
     * 通过关键字像服务器请求搜索内容
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void keyWordRequest(WordMsgEvent event) {
        mEvent = event;
        mType = event.getType();
        fetchData();
    }

    @Override
    public void fetchData() {
        if (null != mAdapter) {
            doKeyWordRequest();
        }
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_search_word;
    }

    @Override
    protected void initData() {
        mAdapter = new BaseRecyclerAdapter<String>(simple_list_item_1) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, String model, int position) {
                holder.text(android.R.id.text1, model);
            }
        };
        mAdapter.setOnItemClickListener((parent, view, position, id) -> {
            String content = ((TextView) view).getText().toString().trim();
            MsgDispatcher msgDispatcher = new MsgDispatcher(SearchFragment.RESULT);
            msgDispatcher.setWordMsgEvent(new WordMsgEvent(content, mType));
            EventBus.getDefault().post(msgDispatcher);
        });
        mRcv.setItemAnimator(new DefaultItemAnimator());
        mRcv.addItemDecoration(new DividerItemDecoration(getContext(), VERTICAL));
        mRcv.setAdapter(mAdapter);
    }

    @Override
    protected void initView() {

    }

    /**
     * 向服务器请求关键搜索内容
     */
    private void doKeyWordRequest() {
        if (null != mEvent) {
            HashMap<String, Object> params = new HashMap<>();
            params.put(KEY_ANTISTOP, mEvent.getMsg());
            params.put(KEY_TYPE, mEvent.getType());
            JSONObject jsonObject = new JSONObject(params);
            getApiHelper().appGetNameAndTitle(jsonObject.toString(), data -> {
                mAdapter.refresh(data);
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
